#ifndef DATAMAPTEST_HPP
#define DATAMAPTEST_HPP

#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cppunit/extensions/HelperMacros.h>
#include "datamap.hpp"

class DataMapTest : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE( DataMapTest );
  CPPUNIT_TEST( test_readingData );
  CPPUNIT_TEST( test_mergingData );
  CPPUNIT_TEST( test_reversedMergingData );
  CPPUNIT_TEST( test_printMergedData );
  CPPUNIT_TEST( test_mergedTableElements );
  CPPUNIT_TEST_SUITE_END();
  
public:
  void setUp();
  void tearDown();
  void test_readingData();
  void test_mergingData();
  void test_reversedMergingData();
  void test_printMergedData();
  void test_mergedTableElements();
};

void DataMapTest::setUp() {}
void DataMapTest::tearDown() {}
 
void DataMapTest::test_readingData() {

  string fileName = "A2by5.tsv";

  DataMap::DataMap dataMap('\t',"NA");
  dataMap.merge(fileName);

  CPPUNIT_ASSERT( dataMap.data_.size() == 2 );

  for ( map<size_t,map<size_t,string> >::const_iterator rowIt( dataMap.data_.begin() ); rowIt != dataMap.data_.end(); ++rowIt) {

    CPPUNIT_ASSERT( rowIt->second.size() == 5 );

  }

  for ( size_t i = 0; i < dataMap.rowKeys_.size(); ++i ) {
    for ( size_t j = 0; j < dataMap.colKeys_.size(); ++j ) {

      CPPUNIT_ASSERT( dataMap.data_.find(i) != dataMap.data_.end() );
      CPPUNIT_ASSERT( dataMap.data_[i].find(j) != dataMap.data_[i].end() );
      
    }
  }

}

void DataMapTest::test_mergingData() {

  string fileName1 = "A2by5.tsv";
  string fileName2 = "B2by4.tsv";

  DataMap::DataMap dataMap('\t',"NA");
  
  dataMap.merge(fileName1);

  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == dataMap.data_.size() );
  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == 2 );
  CPPUNIT_ASSERT( dataMap.colKeys_.size() == 5 );

  dataMap.merge(fileName2);

  CPPUNIT_ASSERT( dataMap.data_.size() == 2 + 2 );
  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == dataMap.data_.size() );
  CPPUNIT_ASSERT( dataMap.colKeys_.size() == 5 + 2 );

}

void DataMapTest::test_reversedMergingData() {

  string fileName1 = "A2by5.tsv";
  string fileName2 = "B2by4.tsv";

  DataMap::DataMap dataMap('\t',"NA");

  dataMap.merge(fileName2);

  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == dataMap.data_.size() );
  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == 2 );
  CPPUNIT_ASSERT( dataMap.colKeys_.size() == 4 );

  dataMap.merge(fileName1);

  CPPUNIT_ASSERT( dataMap.data_.size() == 2 + 2 );
  CPPUNIT_ASSERT( dataMap.rowKeys_.size() == dataMap.data_.size() );
  CPPUNIT_ASSERT( dataMap.colKeys_.size() == 4 + 3 );

}

void DataMapTest::test_mergedTableElements() {

  

  //CPPUNIT_ASSERT( dataMap.at("\"N;LIPD;104;PC(36:5)\"",) == "18,681");


}

void DataMapTest::test_printMergedData() {
  
  string fileName1 = "A2by5.tsv";
  string fileName2 = "B2by4.tsv";

  DataMap::DataMap dataMap('\t',"NA");

  dataMap.merge(fileName1);
  dataMap.merge(fileName2);

  ofstream os("AB.tsv");

  dataMap.printToStream(os);

}



// Registers the fixture into the test 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( DataMapTest ); 

#endif // DATAMAPTEST_HPP
